// $Id: MetaData.java 12 2009-01-04 00:03:18Z moyekj@yahoo.com $

package com.hme.tivo.videostream;

import java.io.FileReader;
import java.io.IOException;
import java.io.BufferedReader;
import java.util.Hashtable;

import com.tivo.hme.bananas.BScreen;

public class MetaData {
   private Hashtable<String,String> data = null;

   // Constructor creates data hash if file metadata file exists
   public MetaData(String fileName) {
      debug.print("fileName=" + fileName);
      String metaFileName = fileName + ".txt";
      data = parseMetaData(metaFileName);
      if (data != null) data.put("fileName", fileName);
   }

   // Read metadata file and store in data   
   public Hashtable<String,String> parseMetaData(String file) {
      debug.print("file=" + file);
      Hashtable<String,String> hash = new Hashtable<String,String>();
      try {
         BufferedReader config = new BufferedReader(
            new FileReader(file)
         );
         System.out.println(">> Reading metadata file: " + file);
         try {
            String line = null;
            String key = null;
            String value = null;
            while (( line = config.readLine()) != null) {
               if (line.length() == 0) continue; // skip empty lines
               if (line.matches("^\\s*.+\\s+:\\s+.+$")) {
                  key = line.replaceFirst("^\\s*(.+)\\s+:\\s+.+$", "$1");
                  value = line.replaceFirst("^\\s*.+\\s+:\\s+(.+)$", "$1");
                  value = value.replaceFirst("^(.+)\\|(.+)$", "$2 $1");
                  if ( hash.containsKey(key) ) {
                     hash.put(key, hash.get(key) + ", " + value);
                  } else {
                     hash.put(key, value);
                  }
                  continue; // parse next line
               }
            }
         }
         finally {
            config.close();
         }
      }
      catch (IOException ex) {
         return null;
      }

      return hash;
   }

   // Create the text display   
   public VText createInfoText(BScreen screen) {
      debug.print("screen=" + screen);
      Hashtable<String,String> map = new Hashtable<String,String>();
      map.put("fileName", "File Name: ");
      map.put("title", "Title: ");
      map.put("originalAirDate", "Originally Aired: ");
      map.put("duration", "Duration: ");
      map.put("callsign", "Channel Name: ");
      map.put("description", "Description: ");
      map.put("vActor", "Actors: ");
      map.put("vExecProducer", "Executive Producers: ");
      map.put("vProgramGenre", "Program Genres: ");
      map.put("vSeriesGenre", "Series Genres: ");
      // Want to display information in specific order
      String order[] = {
         "fileName",
         "title", "originalAirDate", "duration", "callsign", "description",
         "vActor", "vExecProducer", "vProgramGenre", "vSeriesGenre"
      };
      
      // Build the multi-line text information in order
      String key;
      String s = "";
      for (int i=0; i<order.length; i++) {
         key = order[i];
         if(data.containsKey(key)) {
            s = s + map.get(key) + data.get(key) + "\n";
         }
      }
      
      if (GLOBAL.playList_index > -1) {
         int index = GLOBAL.playList_index + 1;
         s = "Playlist item " +
            index +
            "/" +
            GLOBAL.playList.size() +
            "\n" +
            s;
      }
      
      // Create the screen label and return it
      VText info = new VText(
         screen.getNormal(), BScreen.SAFE_TITLE_H, BScreen.SAFE_ACTION_V,
         data.size()+1, "small"
      );
      info.setVisible(false);
      info.setValue(s);
      info.setFlags(BScreen.RSRC_HALIGN_LEFT | BScreen.RSRC_TEXT_WRAP);
      return info;
   }

   // If metadata file exists this returns true, else false   
   public Boolean exists() {
      debug.print("");
      if (data != null)
         return true;
      else
         return false;
   }
}
